package com.zyy.smartcity1129.movie;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieFilmInfo;
import com.zyy.smartcity1129.data.MoviePreViewInfo;
import com.zyy.smartcity1129.data.MovieTheatreInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: MovieMovieFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 19/11/2021$
 * @Version: 1.0
 */
public class MovieMovieFragment extends BaseFragment {
    private SearchView searchView;
    private TextView tvMovie, tvCinema, tvNext;
    private RecyclerView rv;
    private LinearLayout lineChoose;
    private Spinner spinner;
    private TextView tvDistance;
    private String area;
    ExecutorService executor = Executors.newFixedThreadPool(3);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    List<MovieFilmInfo.RowsBean> filmList = new ArrayList<>();
    List<MovieTheatreInfo.RowsBean> theatreList = new ArrayList<>();
    List<MoviePreViewInfo.RowsBean> previewList = new ArrayList<>();
    RvAd<MovieTheatreInfo.RowsBean> rvTheatreAd;
    RvAd<MovieFilmInfo.RowsBean> rvFilmAd;
    RvAd<MoviePreViewInfo.RowsBean> rvPreViewAd;
    int isChick = 0;
    public static int movieId;
    public static String movieName;
    public static int movieNextId;
    public static String movieNextName;
    public static int movieTheatreId;
    public static String movieTheatreName;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_movie;
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        MovieFragment.mutableLiveData.postValue("电影/影院");
        initView(v);
        lineChoose.setVisibility(View.GONE);
        tvMovie.setTextColor(Color.RED);
        tvCinema.setTextColor(Color.BLACK);
        tvNext.setTextColor(Color.BLACK);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                filmList = Http.getInstance().get("/prod-api/api/movie/film/list", null, MovieFilmInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                theatreList = Http.getInstance().get("/prod-api/api/movie/theatre/list", null, MovieTheatreInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                previewList = Http.getInstance().get("/prod-api/api/movie/film/preview/list", null, MoviePreViewInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (atomicInteger.get() == 3) {
                rvFilmAd = new RvAd<MovieFilmInfo.RowsBean>(filmList, R.layout.rv_ad) {
                    private LinearLayout rv_line;
                    private LinearLayout rv_line1;
                    private LinearLayout rv_line2;
                    private TextView rv_tv1;
                    private TextView rv_tv2;
                    private TextView rv_tv3;
                    private TextView rv_tv4;
                    private TextView rv_tv5;
                    private TextView rv_tv6;
                    private TextView rv_tv7;
                    private TextView rv_tv8;
                    private TextView rv_tv9;
                    private TextView rv_tv11;
                    private ImageView rv_ig;

                    private void initView(View v) {
                        rv_line = (LinearLayout) v.findViewById(R.id.rv_line);
                        rv_line1 = (LinearLayout) v.findViewById(R.id.rv_line1);
                        rv_line2 = (LinearLayout) v.findViewById(R.id.rv_line2);
                        rv_ig = (ImageView) v.findViewById(R.id.rv_ig);
                        rv_tv1 = (TextView) v.findViewById(R.id.rv_tv1);
                        rv_tv2 = (TextView) v.findViewById(R.id.rv_tv2);
                        rv_tv3 = (TextView) v.findViewById(R.id.rv_tv3);
                        rv_tv4 = (TextView) v.findViewById(R.id.rv_tv4);
                        rv_tv5 = (TextView) v.findViewById(R.id.rv_tv5);
                        rv_tv6 = (TextView) v.findViewById(R.id.rv_tv6);
                        rv_tv7 = (TextView) v.findViewById(R.id.rv_tv7);
                        rv_tv8 = (TextView) v.findViewById(R.id.rv_tv8);
                        rv_tv9 = (TextView) v.findViewById(R.id.rv_tv9);
                        rv_tv11 = (TextView) v.findViewById(R.id.rv_tv11);
                    }

                    @Override
                    protected void initR(View iv, MovieFilmInfo.RowsBean rowsBean, int p) {
                        initView(iv);
                        rv_line1.setVisibility(View.GONE);
                        rv_tv1.setTextSize(20);
                        rv_tv11.setText("购票");
                        rv_tv11.setBackground(getResources().getDrawable(R.color.sell));
                        rv_tv1.setText(rowsBean.getName());
                        rv_tv2.setText("评分：" + rowsBean.getScore());
                        rv_tv3.setText("上映时间：" + rowsBean.getPlayDate());
                        rv_tv8.setText(String.valueOf(rowsBean.getFavoriteNum()));
                        rv_tv9.setText(String.valueOf(rowsBean.getLikeNum()));
                        Glide.with(rv_ig.getContext()).load(Http.base + rowsBean.getCover()).into(rv_ig);
                        iv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                movieId = rowsBean.getId();
                                movieName = rowsBean.getName();
                                add(new MovieContextFragment());
                            }
                        });
                    }
                };

                rvPreViewAd = new RvAd<MoviePreViewInfo.RowsBean>(previewList, R.layout.rv_ad) {
                    private LinearLayout rv_line;
                    private LinearLayout rv_line1;
                    private LinearLayout rv_line2;
                    private TextView rv_tv1;
                    private TextView rv_tv2;
                    private TextView rv_tv3;
                    private TextView rv_tv4;
                    private TextView rv_tv5;
                    private TextView rv_tv6;
                    private TextView rv_tv7;
                    private TextView rv_tv8;
                    private TextView rv_tv9;
                    private TextView rv_tv11;
                    private ImageView rv_ig;

                    private void initView(View v) {
                        rv_line = (LinearLayout) v.findViewById(R.id.rv_line);
                        rv_line1 = (LinearLayout) v.findViewById(R.id.rv_line1);
                        rv_line2 = (LinearLayout) v.findViewById(R.id.rv_line2);
                        rv_ig = (ImageView) v.findViewById(R.id.rv_ig);
                        rv_tv1 = (TextView) v.findViewById(R.id.rv_tv1);
                        rv_tv2 = (TextView) v.findViewById(R.id.rv_tv2);
                        rv_tv3 = (TextView) v.findViewById(R.id.rv_tv3);
                        rv_tv4 = (TextView) v.findViewById(R.id.rv_tv4);
                        rv_tv5 = (TextView) v.findViewById(R.id.rv_tv5);
                        rv_tv6 = (TextView) v.findViewById(R.id.rv_tv6);
                        rv_tv7 = (TextView) v.findViewById(R.id.rv_tv7);
                        rv_tv8 = (TextView) v.findViewById(R.id.rv_tv8);
                        rv_tv9 = (TextView) v.findViewById(R.id.rv_tv9);
                        rv_tv11 = (TextView) v.findViewById(R.id.rv_tv11);
                    }

                    @Override
                    protected void initR(View iv, MoviePreViewInfo.RowsBean rowsBean, int p) {
                        initView(iv);
                        rv_line1.setVisibility(View.GONE);
                        rv_line2.setVisibility(View.GONE);
                        rv_tv2.setVisibility(View.GONE);
                        rv_tv1.setTextSize(20);
                        rv_tv11.setText("预售");
                        rv_tv11.setBackground(getResources().getDrawable(R.color.forsell));
                        rv_tv1.setText(rowsBean.getName());
                        rv_tv3.setText("上映时间：" + rowsBean.getPlayDate());
                        Glide.with(rv_ig.getContext()).load(Http.base + rowsBean.getCover()).into(rv_ig);
                        iv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                movieNextId = rowsBean.getId();
                                movieNextName = rowsBean.getName();
                                add(new MovieNextFragment());
                            }
                        });
                    }
                };
                initAdapterTheatre(theatreList);
                rv.setLayoutManager(new LinearLayoutManager(getContext()));
                rv.setAdapter(rvFilmAd);
                tvMovie.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvMovie.setTextColor(Color.RED);
                        tvCinema.setTextColor(Color.BLACK);
                        tvNext.setTextColor(Color.BLACK);
                        lineChoose.setVisibility(View.GONE);
                        rv.setAdapter(rvFilmAd);
                    }
                });
                tvCinema.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvMovie.setTextColor(Color.BLACK);
                        tvCinema.setTextColor(Color.RED);
                        tvNext.setTextColor(Color.BLACK);
                        lineChoose.setVisibility(View.VISIBLE);
                        rv.setAdapter(rvTheatreAd);
                    }
                });
                tvNext.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvMovie.setTextColor(Color.BLACK);
                        tvCinema.setTextColor(Color.BLACK);
                        tvNext.setTextColor(Color.RED);
                        lineChoose.setVisibility(View.GONE);
                        rv.setAdapter(rvPreViewAd);
                    }
                });
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        area = spinner.getSelectedItem().toString();
                        switch (area) {
                            case "沙河口区":
                                initAreaList("沙河口区");
                                break;
                            case "甘井子区":
                                initAreaList("甘井子区");
                                break;
                            case "西岗区":
                                initAreaList("西岗区");
                                break;
                            case "中山区":
                                initAreaList("中山区");
                                break;
                            case  "/":
                                initAdapterTheatre(theatreList);
                                rv.setAdapter(rvTheatreAd);
                                break;
                            default:
                                Log.i("TAG", "more area");
                                break;
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                tvDistance.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isChick == 0) {
                            tvDistance.setTextColor(Color.RED);
                            isChick = 1;
                            List<MovieTheatreInfo.RowsBean> list = new ArrayList<>();
                            list.addAll(theatreList);
                            Collections.sort(list, new Comparator<MovieTheatreInfo.RowsBean>() {
                                @Override
                                public int compare(MovieTheatreInfo.RowsBean o1, MovieTheatreInfo.RowsBean o2) {
                                    String o1s = o1.getDistance();
                                    String o2s = o2.getDistance();
                                    if (o1.getName().equals("北联影城")) {
                                        o1s = o1s + "米";
                                    }
                                    if (o2.getName().equals("北联影城")) {
                                        o2s = o2s + "米";
                                    }
                                    int o1d = Integer.parseInt(o1s.substring(0, o1s.length() - 1));
                                    int o2d = Integer.parseInt(o2s.substring(0, o2s.length() - 1));
                                    if (o1d > o2d) {
                                        return 1;
                                    } else if (o1d < o2d) {
                                        return -1;
                                    }
                                    return 0;
                                }
                            });
                            initAdapterTheatre(list);
                        } else {
                            tvDistance.setTextColor(Color.BLACK);
                            isChick = 0;
                            initAdapterTheatre(theatreList);
                        }
                        rv.setAdapter(rvTheatreAd);
                    }
                });
            }
        });
    }

    private void initAreaList(String area) {
        List<MovieTheatreInfo.RowsBean> list = new ArrayList<>();
        for (int i = 0; i < theatreList.size(); i++) {
            if (theatreList.get(i).getArea().equals(area)) {
                list.add(theatreList.get(i));
            }
        }
        initAdapterTheatre(list);
        rv.setAdapter(rvTheatreAd);
    }


    private void initAdapterTheatre(List<MovieTheatreInfo.RowsBean> theatreList) {
        rvTheatreAd = new RvAd<MovieTheatreInfo.RowsBean>(theatreList, R.layout.rv_theatre) {
            private TextView rv_tvname;
            private TextView rv_tvadd;
            private TextView rv_tvscore;
            private TextView rv_tvdistance;
            private void initView(View v) {
                rv_tvname = (TextView) v.findViewById(R.id.rv_tvname);
                rv_tvadd = (TextView) v.findViewById(R.id.rv_tvadd);
                rv_tvscore = (TextView) v.findViewById(R.id.rv_tvscore);
                rv_tvdistance = (TextView) v.findViewById(R.id.rv_tvdistance);
            }
            @Override
            protected void initR(View iv, MovieTheatreInfo.RowsBean rowsBean, int p) {
                initView(iv);
                rv_tvname.setText(rowsBean.getName());
                rv_tvadd.setText(rowsBean.getArea() + rowsBean.getAddress());
                rv_tvscore.setText("评分：" + rowsBean.getScore());
                rv_tvdistance.setText(rowsBean.getDistance());
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        movieTheatreId = rowsBean.getId();
                        movieTheatreName = rowsBean.getName();
                        add(new MovieTheatreFragment());
//                        Toast.makeText(getContext(), rowsBean.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
    }

    private void initView(View v) {
        tvDistance = v.findViewById(R.id.tvDistance);
        spinner = v.findViewById(R.id.spinner);
        lineChoose = v.findViewById(R.id.lineChoose);
        searchView = v.findViewById(R.id.searchView);
        tvMovie = v.findViewById(R.id.tvMovie);
        tvCinema = v.findViewById(R.id.tvCinema);
        tvNext = v.findViewById(R.id.tvNext);
        rv = v.findViewById(R.id.rv);
    }
}
